package com.adam.springmvc.service;

import com.adam.springmvc.generator.mapper.CourseMapper;
import com.adam.springmvc.generator.model.Course;
import com.adam.springmvc.generator.model.CourseExample;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class CourseService {

    private final Logger logger = LoggerFactory.getLogger(CourseService.class);

    @Autowired
    private CourseMapper courseMapper;

    public Course findCourseById(int id) {
        logger.debug("findCourseById:{}", id);
        return courseMapper.selectByPrimaryKey(id);
    }

    public List<Course> findCoursesByDate(Date startDate, Date endDate) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        logger.debug("findCoursesByDate:{} {}", simpleDateFormat.format(startDate), simpleDateFormat.format(endDate));
        CourseExample example = new CourseExample();
        example.createCriteria().andStartDateGreaterThanOrEqualTo(startDate)
                .andEndDateLessThanOrEqualTo(endDate);
        return courseMapper.selectByExample(example);
    }

}
